<?php
$startTime = microtime(true);
include './ZzL/Conf.php';
include './ZzL/Right.php';
include './ZzL/autoload.php';
session_start();
//Le répertoire courant
	$GLOBALS['cwd'] = getcwd();

//test JSON 
$jsonQuery = stripslashes($_POST['query']);/*'
{
"id":1,
"getVal":["name"],
"getChildren": {
					"type":"article",
					"nbChildrenPerPage":10,
					"numPage":0,
					"order":"normal",
					"val":["id","name"],
					"ref":[{"ref":"test","operator":"<=","value":10}]
					},
"setVal": {
					"name":"puZZle project home page"
					}
}';

/*
$jsonQuery = '
{
"id":1,
"getVal":["name"],
"getChildren": {
					"type":"article",
					"nbChildrenPerPage":10,
					"numPage":0,
					"order":"normal",
					"val":["id","name"],
					"ref":[{"ref":"test","operator":"<=","value":10}]
					}
}';//*/
//echo '<pre>';
// Array Query
$arrayQuery = json_decode($jsonQuery,true);

if($arrayQuery === null)
	echo syntaxError();
else
	echo execQuery($arrayQuery);


function execQuery($arrayQuery) {
	// result Query
		$result = array();
		$result['query'] = $arrayQuery;
	// All is Ok, no errors
		$result['status'] = 'ok';

	// Make ZZIO Query
	if(!isSet($arrayQuery['id'])) {
		syntaxError();
		return;
	}
	// Create ZzIO instance
		$IO = ZzIO::getInstance($arrayQuery['id']);
		$IO->noRedirection();


	// Set Val
		if(isSet($arrayQuery['setVal'])) {
			// Right ?
			if(!$IO->writable) {
				notWritableError();
				return;
			}
			foreach($arrayQuery['setVal'] as $key => $val) {
				$IO->need($key);
				$_POST[$key] = $val;
			}
			if($IO->isNotCommitable()) { 
				formatError();
				return;
			}
			$IO->commit();
		}
		
	// GetVal
		if(isSet($arrayQuery['getVal'])) {
			// Right ?
			if(!$IO->readable) {
				notReadableError();
				return;
			}

			foreach($arrayQuery['getVal'] as $val) {
				$IO->need($val);
			}
			$result['value'] = array();
			foreach($arrayQuery['getVal'] as $val)
				$result['value'][$val] =  $IO->get($val)->value;
		}

	// Get Children
		if(isSet($arrayQuery['getChildren'])) {
			//Type
				if(isSet($arrayQuery['getChildren']['type']))
					$type = $arrayQuery['getChildren']['type'];
				else
					$type = null;
			//nbChildren
				if(isSet($arrayQuery['getChildren']['nbChildrenPerPage'])) {
					if($arrayQuery['getChildren']['nbChildrenPerPage'] == 'noLimit')
						$noLimit = true;
					else {
						$IO->setNbChildrenPerPage(intval($arrayQuery['getChildren']['nbChildrenPerPage']));
						$noLimit = false;
					}
				}
			// Page number
				if(isSet($arrayQuery['getChildren']['numPage'])) {
					$GET['page'] = $child['numPage'];
				}
				$result['children'] = array();

			// Criteria
				if(isSet($arrayQuery['getChildren']['criteria'])) {
					foreach($arrayQuery['getChildren']['criteria'] as $ref) {
						if(is_null($ref['ref']) or is_null($ref['operator']) or is_null($ref['value'])) {
							syntaxError();
							return;
						}
						$IO->defCriteria($ref['ref'],$ref['operator'],$ref['value']);
					}
				}

			// Ref
				if(isSet($arrayQuery['getChildren']['ref'])) {
					foreach($arrayQuery['getChildren']['ref'] as $ref) {
						if(is_null($ref['ref']) or is_null($ref['value'])) {
							syntaxError();
							return;
						}
						$IO->setRef($ref['ref'],$ref['value']);
					}
				}

			// UserRef
				if(isSet($arrayQuery['getChildren']['userRef'])) {
					foreach($arrayQuery['getChildren']['userRef'] as $ref) {
						if(is_null($ref['ref']) or is_null($ref['value'])) {
							syntaxError();
							return;
						}
						$IO->setUserRef($ref['ref'],$ref['value']);
					}
				}

			//Query
				if($noLimit)
					$_children = $IO->getAllChildren($type);
				else
					$_children = $IO->getChildren($type);

			foreach($_children as $child) {
				$result['children'][$child->id] = array();
				if(isSet($arrayQuery['getChildren']['val'])) {
					foreach($arrayQuery['getChildren']['val'] as $val)
						$child->need($val);
					foreach($arrayQuery['getChildren']['val'] as $val)
						$result['children'][$child->id][$val] =  $child->get($val)->value;
				}
			}
		}
	// Length
		global $startTime;
		$result['duration'] = microtime(true)-$startTime;
	//result
		return json_encode($result);
}

function syntaxError() {
	echo '{"status":"error","error":"syntax"}';
}
function notReadableError() {
	echo '{"status":"error","error":"notReadable"}';
}
function notWritableError() {
	echo '{"status":"error","error":"notWritable"}';
}
function formatError() {
	echo '{"status":"error","error":"format"}';
}
?>